//
//  Utility.m
//  MoneyLog
//
//  Created by wicresoft on 11-1-28.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Utility.h"


@implementation Utility

-(id) init{
	[super init];
	
	return self;
}

//print log
+ (void) printLog:(NSString *)tag Log:(NSString *)log {
	NSLog(@"%@:%@",tag,log);
}

//显示log
+(void) showLog:(NSString *)log{
	//在debug窗口打印出log
	NSLog(@"%@",log);
}

+(NSDate *)getDateByNextDay:(int *)year month:(int *)month day:(int *)day
{
    assert(*year !=0);
    assert(*month >=0 && *month <=12);
    assert(*day>=0 && *day<=31);
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate new]];
    
    [dateComponents setYear:*year];
    [dateComponents setMonth:*month];
    [dateComponents setDay:*day];
    
    NSDate *date =[dateComponents date];
    
    [calendar release];
    return date;
}

+ (NSArray *)colorArray {
    NSArray *colorArray=[NSArray arrayWithObjects:UIColorFromRGB(0x724d9a),UIColorFromRGB(0x48549c),UIColorFromRGB(0x4173ba),UIColorFromRGB(0x47a3ae),
                         UIColorFromRGB(0x0a7d42),UIColorFromRGB(0x639b44),UIColorFromRGB(0xd1c02a),UIColorFromRGB(0xdb9728),
                         UIColorFromRGB(0xc46d2a),UIColorFromRGB(0xed6952),UIColorFromRGB(0xea4043),UIColorFromRGB(0x87266b),nil];
    
    return [[colorArray retain] autorelease];
}


-(void) dealloc{
	[super dealloc];
}

@end


#pragma mark - * 由经纬度获取地图

float MercatorToNormal( float y){
	y = sin( -1.0f * y * 4.0f * atan(1) / 180.0f );
	y = (1.0f + y ) / ( 1.0f - y );
	y = 0.5 * log(y);
	y = y * 1.0 / (2.0f * 4.0f *atan(1.0f));
	return(y + 0.5);
    
}

float NormalToMercator( float y){
	y = y - 0.5f;
	y = y * (2.0f * 4.0f * atan(1.0f) );
	y = expf(y * 2.0f );
	y = ( y - 1.0f ) / ( y  + 1.0f );
	y = atan( y / sqrt( -1.0f * y * y + 1.0f ) );
	return( y * -180.0f /(4.0f *atan(1.0f)) );
    
}

int GetQuadtreeAddress(float lat, float lon, int zoom, char *quad){   
	char 	lookup[4] = { 'q', 'r', 't', 's' };
	float 	x, y;
	int	i, b;
    
    
	quad[0] = 't';
	x 	= ( 180.0f + lon ) / 360.0f;
	y	= MercatorToNormal(lat);
    
	for (i = 1 ; i < zoom ; i++){
		b = 0;
		x = x - (int)x;
		y = y - (int)y;
		if ( x >= 0.5f ) b += 1;
		if ( y >= 0.5f ) b += 2;
		quad[i] = lookup[b];
		x *= 2;
		y *= 2;
        
	}
	return(0);
}

//----------------------------------------------------------------------------------------------------//
int GetCoordinatesFromAddress(char *str, int zoom, float *output){
    
	// get normalized coordinate first
    float 	x 	= 0.0f;
    float	y 	= 0.0f;
	float	scale 	= 1.0f;
	float	lon_min, lat_min, lon_max, lat_max, lon, lat;
    
	int	i;
    
	for ( i = 1 ; i < zoom ; i++ ){
		scale *= 0.5;
		if ( ( str[i] == 'r' ) || ( str[i] == 's' ) ) x += scale;
		if ( ( str[i] == 't' ) || ( str[i] == 's' ) ) y += scale;
	}
    
    
	output[0] = lon		= (x + scale * 0.5f - 0.5f) * 360.0f;
	output[1] = lat		= NormalToMercator( y + scale * 0.5 );
    
	output[2] = lon_min	= (x - 0.5f) * 360.0f;
	output[3] = lat_min	= NormalToMercator(y);
    
	output[4] = lon_max	= ( x + scale - 0.5f ) * 360.0f;
	output[5] = lat_max	= NormalToMercator( y + scale );
    
    
	return(0);
}
